package com.mysteel.cyx.demo.biz.export;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.google.common.collect.Lists;
import com.mysteel.cyx.demo.biz.export.bo.AssetAllocationBO;
import com.mysteel.cyx.demo.biz.export.bo.ComplianceMonitoringDailyB0;
import com.mysteel.cyx.demo.biz.export.bo.ComplianceMonitoringErrorBO;
import com.mysteel.cyx.demo.biz.export.bo.ComplianceMonitoringWarnB0;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 合规监控日报导出!
 *
 * @author 陈烟星
 * date 2024/6/18 9:08
 */
public class complianceMonitoringDailyReport {

    public void exportComplianceMonitoringDailyReport(ExcelWriter excelWriter, ComplianceMonitoringDailyB0 complianceMonitoringDailyB0) throws IOException {

        WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build();

        ExcelUtil.writeTitle(excelWriter, writeSheet, Lists.newArrayList("监控日报"), Lists.newArrayList("业务日期：" + complianceMonitoringDailyB0.getBusinessDate()));

        WriteSheetHolder writeSheetHolder = excelWriter.writeContext().writeSheetHolder();
        Sheet sheet = writeSheetHolder.getSheet();

        // 写入资产配置情况
        ExcelUtil.writeBoldLeftAlignedTitle(excelWriter, writeSheet, sheet, Lists.newArrayList(StringUtils.EMPTY), Lists.newArrayList("一、资产配置情况"));

        // A对象数据
        List<AssetAllocationBO> assetAllocationBoList = complianceMonitoringDailyB0.getAssetAllocationBoList();
        List<List<AssetAllocationBO>> lists = Lists.partition(assetAllocationBoList, 1);
        for (List<AssetAllocationBO> subAssetAllocationBOList : lists) {
            ExcelUtil.writeData(subAssetAllocationBOList, AssetAllocationBO.class, excelWriter, writeSheet, 0, true);
        }
        // 计算并写入合计行
        sumByCell(excelWriter, assetAllocationBoList);

        ExcelUtil.writeBoldLeftAlignedTitle(excelWriter, writeSheet, sheet, Lists.newArrayList(StringUtils.EMPTY), Lists.newArrayList("二、合规监控异常情况"));

        // B对象数据
        ExcelUtil.writeData(complianceMonitoringDailyB0.getComplianceMonitoringErrorBoList(), ComplianceMonitoringErrorBO.class, excelWriter, writeSheet, 1, false);

        // 合规监控预警情况
        ExcelUtil.writeBoldLeftAlignedTitle(excelWriter, writeSheet, sheet, Lists.newArrayList(StringUtils.EMPTY), Lists.newArrayList("三、合规监控预警情况"));

        // C对象数据
        ExcelUtil.writeData(complianceMonitoringDailyB0.getComplianceMonitoringWarnBoList(), ComplianceMonitoringWarnB0.class, excelWriter, writeSheet, 2, false);

    }

    private void sumByCell(ExcelWriter excelWriter, List<AssetAllocationBO> assetAllocationBoList) {
        WriteSheetHolder writeSheetHolder = excelWriter.writeContext().writeSheetHolder();
        Sheet sheet = writeSheetHolder.getSheet();
        int lastRowNum = sheet.getLastRowNum();
        Row totalRow = sheet.createRow(lastRowNum + 1);
        Cell totalCell0 = totalRow.createCell(0);
        totalCell0.setCellValue("合计");

        //资产净值sum
        Cell totalCell1 = totalRow.createCell(4);
        BigDecimal sum1 = assetAllocationBoList.stream().filter(Objects::nonNull).map(AssetAllocationBO::getFundNetValue).reduce(BigDecimal.ZERO, BigDecimal::add);
        totalCell1.setCellValue(sum1.toString());
        //股票
        Cell totalCell2 = totalRow.createCell(5);
        BigDecimal sum2 = assetAllocationBoList.stream().filter(Objects::nonNull).map(AssetAllocationBO::getStockFullPriceAsset).reduce(BigDecimal.ZERO, BigDecimal::add);
        totalCell2.setCellValue(sum2.toString());
        Cell totalCell3 = totalRow.createCell(6);
        BigDecimal sum3 = assetAllocationBoList.stream().filter(Objects::nonNull).map(AssetAllocationBO::getStockFullAstNavRatio).reduce(BigDecimal.ZERO, BigDecimal::add);
        totalCell3.setCellValue(sum3.toString());
        //债券
        Cell totalCell4 = totalRow.createCell(7);
        BigDecimal sum4 = assetAllocationBoList.stream().filter(Objects::nonNull).map(AssetAllocationBO::getBondFullPriceAsset).reduce(BigDecimal.ZERO, BigDecimal::add);
        totalCell4.setCellValue(sum4.toString());
        Cell totalCell5 = totalRow.createCell(8);
        BigDecimal sum5 = assetAllocationBoList.stream().filter(Objects::nonNull).map(AssetAllocationBO::getBondFullAstNavRatio).reduce(BigDecimal.ZERO, BigDecimal::add);
        totalCell5.setCellValue(sum5.toString());
        //基金(除货币基金)
        Cell totalCell6 = totalRow.createCell(9);
        BigDecimal sum6 = assetAllocationBoList.stream().filter(Objects::nonNull).map(AssetAllocationBO::getFundFullPriceAsset).reduce(BigDecimal.ZERO, BigDecimal::add);
        totalCell6.setCellValue(sum6.toString());
        Cell totalCell7 = totalRow.createCell(10);
        BigDecimal sum7 = assetAllocationBoList.stream().filter(Objects::nonNull).map(AssetAllocationBO::getFundFullAstNavRatio).reduce(BigDecimal.ZERO, BigDecimal::add);
        totalCell7.setCellValue(sum7.toString());
        //现金(含货币基金)
        Cell totalCell8 = totalRow.createCell(11);
        BigDecimal sum8 = assetAllocationBoList.stream().filter(Objects::nonNull).map(AssetAllocationBO::getCashFullPriceAsset).reduce(BigDecimal.ZERO, BigDecimal::add);
        totalCell8.setCellValue(sum8.toString());
        Cell totalCell9 = totalRow.createCell(12);
        BigDecimal sum9 = assetAllocationBoList.stream().filter(Objects::nonNull).map(AssetAllocationBO::getCashFullAstNavRatio).reduce(BigDecimal.ZERO, BigDecimal::add);
        totalCell9.setCellValue(sum9.toString());
        //理财
        Cell totalCell10 = totalRow.createCell(13);
        BigDecimal sum10 = assetAllocationBoList.stream().filter(Objects::nonNull).map(AssetAllocationBO::getFmProductFullPriceAsset).reduce(BigDecimal.ZERO, BigDecimal::add);
        totalCell10.setCellValue(sum10.toString());
        Cell totalCell11 = totalRow.createCell(14);
        BigDecimal sum11 = assetAllocationBoList.stream().filter(Objects::nonNull).map(AssetAllocationBO::getFmProductFullAstNavRatio).reduce(BigDecimal.ZERO, BigDecimal::add);
        totalCell11.setCellValue(sum11.toString());
        //其他
        Cell totalCell12 = totalRow.createCell(15);
        BigDecimal sum12 = assetAllocationBoList.stream().filter(Objects::nonNull).map(AssetAllocationBO::getOtherFullPriceAsset).reduce(BigDecimal.ZERO, BigDecimal::add);
        totalCell12.setCellValue(sum12.toString());
        Cell totalCell13 = totalRow.createCell(16);
        BigDecimal sum13 = assetAllocationBoList.stream().filter(Objects::nonNull).map(AssetAllocationBO::getOtherFullAstNavRatio).reduce(BigDecimal.ZERO, BigDecimal::add);
        totalCell13.setCellValue(sum13.toString());
        //负债
        Cell totalCell14 = totalRow.createCell(17);
        BigDecimal sum14 = assetAllocationBoList.stream().filter(Objects::nonNull).map(AssetAllocationBO::getDebtFullPriceAsset).reduce(BigDecimal.ZERO, BigDecimal::add);
        totalCell14.setCellValue(sum14.toString());
        Cell totalCell15 = totalRow.createCell(18);
        BigDecimal sum15 = assetAllocationBoList.stream().filter(Objects::nonNull).map(AssetAllocationBO::getDebtFullAstNavRatio).reduce(BigDecimal.ZERO, BigDecimal::add);
        totalCell15.setCellValue(sum15.toString());

        sheet.addMergedRegion(new CellRangeAddress(lastRowNum + 1, lastRowNum + 1, 0, 1));
        // 设置合计行单元格样式
        CellStyle boldStyle = ExcelUtil.createCellStyle(sheet.getWorkbook(), "宋体", (short) 10, true, HorizontalAlignment.CENTER);
        totalCell0.setCellStyle(boldStyle);
    }

}


